<?
//<@
import ("Zend_Http_Client");
import ("stock_analyser_AnalyserView");
//@>
import ("stock_price_PriceConfig");
class stock_price_PriceView extends jsml_View {
	public $buttonImport;
	public $divLog;
	public function view() {
		_($this)->bind("rpc.queryImport", function($event, $result){
			$this->divLog->html(JSON::stringify($result));
		});
		$this->buttonImport = _('<button>import</button>')->css($this->config->getCss(null, array(left=>0,top=>0, width=>50, height=>25)));
		$this->buttonImport->bind("click.buttonImport", function($event){
			if(!confirm("import?")) return;
			$this->queryImport();
		});
		$this->div->append($this->buttonImport);

		$aaa = "bbb";
		$this->aaa = "cccc";
		?>
<$html><>GGGGGGG<> RRRRRRRRRRR<>
		<? $this->a = 1000; ?>
<div>Log -> :: . . parent.view(); |<?= $aaa ?></div>
<div>Log -> :: . . parent.view(); |<?= $this->a ?></div>
NNNNNNN<> LLLLLLL<></$html>
		<?
		$this->divLog = _($html)->appendTo($this->div)->css($this->config->getCss(null,array(left=>0,top=>25, width=>$this->div->width()-2, height=>100, border=>'solid 1px')));
		return $this;
	}

	protected $importDir = 'WEB-INF/files/stock/price/';
	protected $year = 2011;

	public function getZipDir(){
		return $this->importDir . "istock_" . $this->year . ".zip";
	}
	public function getHoDir(){
		return $this->importDir . "HO_" . $this->year . ".txt";
	}
	public function getHaDir(){
		return $this->importDir . "HA_" . $this->year . ".txt";
	}

	public function query($param) {
		//<@ $this->jsonp($param);
		return array("aaa" => $param);
		//@>
	}
	protected  $stmCreate;
	public function queryCreate() {
		//<@ $this->jsonp();
		if($this->stmCreate == null){
			$vo = $this->vo;
			$sql = "CREATE TABLE ".get_class($vo)."(
			$vo->stock_symbol VARCHAR(255),
			$vo->stockDate DATETIME,
			$vo->open DOUBLE,
			$vo->high DOUBLE,
			$vo->low DOUBLE,
			$vo->close DOUBLE,
			$vo->volume INT,
			PRIMARY KEY( $vo->stock_symbol,$vo->stockDate )
			) TYPE=MyISAM CHARACTER SET utf8";
			$this->stmCreate = $this->ds->prepare($sql);
		}
		$this->stmCreate->execute();
		//@>
	}

	protected $stmReplace;
	public function queryReplace($priceMap){
		//<@ $this->jsonp($priceMap);
		if($this->stmReplace == null){
			$vo = $this->vo;
			$sql = "REPLACE INTO ".get_class($vo)." SET
			$vo->stock_symbol =:$vo->stock_symbol,
			$vo->stockDate =:$vo->stockDate,
			$vo->open =:$vo->open,
			$vo->high =:$vo->high,
			$vo->low =:$vo->low,
			$vo->close =:$vo->close,
			$vo->volume =:$vo->volume";
			$this->stmReplace = $this->ds->prepare($sql);
		}
		$this->stmReplace->execute($priceMap);
		//@>
	}

	public function queryImport(){
		//<@ $this->jsonp();
/*
		$runtime = microtime(true);	
		$httpClient = new Zend_Http_Client("http://www.cophieu68.com/filemetastock/hose.txt.zip");
		$httpClient->setHeaders(array(
			'Host'=>	'www.cophieu68.com',
			'Accept'=>	'*'.'/*',
			'Referer'=>	'http://www.cophieu68.com/export.php',
			'Connection'=>	'keep-alive',
		));
		$html = $httpClient->request('GET')->getBody();
		$zipDir = $this->importDir . "hose.txt.zip";
		file_put_contents($zipDir, $html);
		if(file_exists($zipDir)){
			$zip = new ZipArchive();
			$res = $zip->open($zipDir);
			$zip->extractTo($this->importDir);
			$zip->close();
		}
		
		$httpClient = new Zend_Http_Client("http://www.cophieu68.com/filemetastock/hnx.txt.zip");
		$httpClient->setHeaders(array(
			'Host'=>	'www.cophieu68.com',
			'Accept'=>	'*'.'/*',
			'Referer'=>	'http://www.cophieu68.com/export.php',
			'Connection'=>	'keep-alive',
		));
		$html = $httpClient->request('GET')->getBody();
		$zipDir = $this->importDir . "hnx.txt.zip";
		file_put_contents($zipDir, $html);
		if(file_exists($zipDir)){
			$zip = new ZipArchive();
			$res = $zip->open($zipDir);
			$zip->extractTo($this->importDir);
			$zip->close();
		}
		
		$httpClient = new Zend_Http_Client("http://www.cophieu68.com/export/metastock.php?id=%5Evnindex");
		$httpClient->setHeaders(array(
			'Host'=>	'www.cophieu68.com',
			'Accept'=>	'*'.'/*',
			'Referer'=>	'http://www.cophieu68.com/export.php',
			'Connection'=>	'keep-alive',
		));
		$html = $httpClient->request('GET')->getBody();
		$zipDir = $this->importDir . "vnindex.txt";
		file_put_contents($zipDir, $html);
		
		$httpClient = new Zend_Http_Client("http://www.cophieu68.com/export/metastock.php?id=%5Ehastc");
		$httpClient->setHeaders(array(
			'Host'=>	'www.cophieu68.com',
			'Accept'=>	'*'.'/*',
			'Referer'=>	'http://www.cophieu68.com/export.php',
			'Connection'=>	'keep-alive',
		));
		$html = $httpClient->request('GET')->getBody();
		$zipDir = $this->importDir . "hastc.txt";
		file_put_contents($zipDir, $html);
	
		$sql = null;
		$dirArray = array(
			//$this->importDir . "filemetastock/hose.txt",
			$this->importDir . "filemetastock/hnx.txt"
			,$this->importDir . "vnindex.txt"
			,$this->importDir . "hastc.txt"
		);
		foreach($dirArray as $i=>$dir){
			if($handle = @fopen($dir, "r", 1)) {
				fgets($handle);//omit first row
				$line=null;
				while (!feof($handle)) {
					$line = trim(fgets($handle));
					if($line=='') continue;

					$priceMap = array();
					$array = explode(',',$line);
					if(substr($array[1],0,4) != $this->year) continue;
					//echo substr($array[1],0,4);
					if($array[0] == 'VF4') $array[0] = 'VFMVF4';
					if($array[0] == 'VF1') $array[0] = 'VFMVF1';
					if($array[0] == 'BF1') $array[0] = 'PRUBF1';
					if($array[0] == 'PF1') $array[0] = 'MAFPF1';
					if($array[0] == '^HASTC') $array[0] = 'HAINDEX';
					if($array[0] == '^VNINDEX') $array[0] = 'VNINDEX';

					$priceMap[$this->vo->stock_symbol] = $array[0];
					$priceMap[$this->vo->stockDate] = implode('-', array( substr($array[1],0,4), substr($array[1],4,2), substr($array[1],6,2) ) );
					$priceMap[$this->vo->open] =  $array[2];
					$priceMap[$this->vo->high] =  $array[3];
					$priceMap[$this->vo->low] =  $array[4];
					$priceMap[$this->vo->close] =  $array[5];
					$priceMap[$this->vo->volume] =  $array[6];
					
					if($priceMap[$this->vo->close] == "0.0" || $priceMap[$this->vo->close] == "") $priceMap[$this->vo->close] = $priceMap[$this->vo->open];
					if($priceMap[$this->vo->low] == "0.0" || $priceMap[$this->vo->low] == "") $priceMap[$this->vo->low] = $priceMap[$this->vo->close];
					if($priceMap[$this->vo->high] == "0.0" || $priceMap[$this->vo->high] == "") $priceMap[$this->vo->high] = $priceMap[$this->vo->close];
					if($priceMap[$this->vo->open] == "0.0" || $priceMap[$this->vo->open] == "") $priceMap[$this->vo->open] = $priceMap[$this->vo->close];
					//print_p($priceMap);
					
					$this->queryReplace($priceMap);
				}
				fclose($handle);
			}
		}
		$analyserView = new stock_analyser_AnalyserView();
		$analyserView->deletePriceDtArrayMap();
		$runtime = microtime(true) - $runtime;
		$result = array();
		$result["runtime"] = $runtime;
		return $result;
*/
		
		

		$runtime = microtime(true);
		$httpClient = new Zend_Http_Client("http://www.istock.vn/GDDDownloader.aspx?id=istock_" . $this->year . ".zip");
		$httpClient->setHeaders(array(
			'Host'=>	'www.istock.vn',
			'Accept'=>	'*'.'/*',
			'Referer'=>	'http://www.istock.vn/gdddownloaded.aspx',
			'Connection'=>	'keep-alive',
		));
		$html = $httpClient->request('GET')->getBody();
		file_put_contents($this->getZipDir(), $html);
		if(file_exists($this->getZipDir())){
			$zip = new ZipArchive();
			$res = $zip->open($this->getZipDir());
			$zip->extractTo($this->importDir);
			$zip->close();
		}

		$sql = null;
		$dirArray = array($this->getHoDir(), $this->getHaDir());
		foreach($dirArray as $i=>$dir){
			if($handle = @fopen($dir, "r", 1)) {
				fgets($handle);//omit first row
				$line=null;
				while (!feof($handle)) {
					$line = trim(fgets($handle));
					if($line=='') continue;

					$priceMap = array();
					$array = explode(',',$line);
					if($array[0] == 'VF4') $array[0] = 'VFMVF4';
					if($array[0] == 'VF1') $array[0] = 'VFMVF1';
					if($array[0] == 'BF1') $array[0] = 'PRUBF1';
					if($array[0] == 'PF1') $array[0] = 'MAFPF1';

					$priceMap[$this->vo->stock_symbol] = $array[0];
					$priceMap[$this->vo->stockDate] = implode('-', array( substr($array[2],0,4), substr($array[2],4,2), substr($array[2],6,2) ) );
					$priceMap[$this->vo->open] =  $array[4];
					$priceMap[$this->vo->high] =  $array[5];
					$priceMap[$this->vo->low] =  $array[6];
					$priceMap[$this->vo->close] =  $array[7];
					$priceMap[$this->vo->volume] =  $array[8];

					$this->queryReplace($priceMap);
				}
				fclose($handle);
			}
		}
		$analyserView = new stock_analyser_AnalyserView();
		$analyserView->deletePriceDtArrayMap();
		$runtime = microtime(true) - $runtime;
		$result = array();
		$result["runtime"] = $runtime;
		return $result;
		//@>
	}
	
	public static function main(){
		_($__document)->ready(function(){
			$config = stock_price_PriceConfig::$config;
			$view = $config->getBean($config->PriceView);
			_($__document->body)->append($view->div);
			$view->view();
		});
	}
}